package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.updateZsService;
import com.clqteam.serviceimpl.updateZsServiceImpl;

public class updataZsController extends HttpServlet{
	 
	           updateZsService upd = new updateZsServiceImpl();
	       
          @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        	  doPost(req, resp);
        }
          
          
          
          @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        	//获取系统时间
         	 Date date=new Date();
         	 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         	 String drTime=format.format(date);
         	 //获取参数
         	String zspIds=req.getParameter("zspId");
       		int zspId  = Integer.parseInt(zspIds);
       		String zspAmounts=req.getParameter("zspAmount");
       		int zspAmount  = Integer.parseInt(zspAmounts);
       		String zsIds=req.getParameter("zsId");
       		int zsId  = Integer.parseInt(zsIds);
       		int oudCount = upd.getOldZsInfoByZspid(zsId);
       		int pCount = upd.getProductCountByZspid(zspId);
       		int newCount = zspAmount-oudCount;
       		int a = pCount - newCount;
       		if (pCount>newCount) {
       		 int s= upd.updateShByZspid(zspId, a);
       		 int ss = upd.updateZsByZspid(zsId, zspAmount);
           	 String mm = Integer.toString(s);
           	 PrintWriter pw = resp.getWriter();
           	 pw.write(mm);
			}else {
				String mm = "0";
				 PrintWriter pw = resp.getWriter();
	           	 pw.write(mm);
			}
       	 //写入日志
          	 HttpSession session = req.getSession();
          	 Employee emp = (Employee) session.getAttribute("employee");
          	 String eName = emp.geteName();
          	 int eId = emp.geteId();
          
          	 StringBuffer drInfos = new StringBuffer();
         		drInfos.append("用户");
         		drInfos.append(eName);
         		drInfos.append("把编号为");
         		drInfos.append(zsId);
         		drInfos.append("的自损订单中自损商品数量由");
         		drInfos.append(oudCount);
         		drInfos.append("改为");
         		drInfos.append(zspAmount);
         		String drInfo = drInfos.toString();
               upd.setdirInfo(drInfo, drTime, eId, eName);
               
               double sms = upd.getPcostfromZs(zsId);
               double oMoney = a*sms;
               int oId = upd.getOidByPid(zspId);
               upd.updataOrder(oId, oMoney);
               upd.updataOrderInfo(oId, a);
         	 
         	 

        	  
        	  
        	  
        }
}
